<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>colorpalette.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_colorpalette.js.html">colorpalette.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A control for representing a palette of colors, that the user
<a name="line17"></a> * can highlight or select via the keyboard or the mouse.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.ColorPalette&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.color&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.style&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.Palette&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.PaletteRenderer&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * A color palette is a grid of color swatches that the user can highlight or
<a name="line34"></a> * select via the keyboard or the mouse.  The selection state of the palette is
<a name="line35"></a> * controlled by a selection model.  When the user makes a selection, the
<a name="line36"></a> * component fires an ACTION event.  Event listeners may retrieve the selected
<a name="line37"></a> * color using the {@link #getSelectedColor} method.
<a name="line38"></a> *
<a name="line39"></a> * @param {Array.&lt;string&gt;=} opt_colors Array of colors in any valid CSS color
<a name="line40"></a> *     format.
<a name="line41"></a> * @param {goog.ui.PaletteRenderer=} opt_renderer Renderer used to render or
<a name="line42"></a> *     decorate the palette; defaults to {@link goog.ui.PaletteRenderer}.
<a name="line43"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
<a name="line44"></a> *     document interaction.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @extends {goog.ui.Palette}
<a name="line47"></a> */
<a name="line48"></a>goog.ui.ColorPalette = function(opt_colors, opt_renderer, opt_domHelper) {
<a name="line49"></a>  /**
<a name="line50"></a>   * Array of colors to show in the palette.
<a name="line51"></a>   * @type {Array.&lt;string&gt;}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.colors_ = opt_colors || [];
<a name="line55"></a>
<a name="line56"></a>  goog.ui.Palette.call(this, null,
<a name="line57"></a>      opt_renderer || goog.ui.PaletteRenderer.getInstance(), opt_domHelper);
<a name="line58"></a>
<a name="line59"></a>  // Set the colors separately from the super call since we need the correct
<a name="line60"></a>  // DomHelper to be initialized for this class.
<a name="line61"></a>  this.setColors(this.colors_);
<a name="line62"></a>};
<a name="line63"></a>goog.inherits(goog.ui.ColorPalette, goog.ui.Palette);
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * Array of normalized colors.  Inited lazily as often never needed.
<a name="line68"></a> * @type {Array.&lt;string&gt;?}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.ui.ColorPalette.prototype.normalizedColors_ = null;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Returns the array of colors represented in the color palette.
<a name="line76"></a> * @return {Array.&lt;string&gt;} Array of colors.
<a name="line77"></a> */
<a name="line78"></a>goog.ui.ColorPalette.prototype.getColors = function() {
<a name="line79"></a>  return this.colors_;
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Sets the colors that are contained in the palette.
<a name="line85"></a> * @param {Array.&lt;string&gt;} colors Array of colors in any valid CSS color format.
<a name="line86"></a> */
<a name="line87"></a>goog.ui.ColorPalette.prototype.setColors = function(colors) {
<a name="line88"></a>  this.colors_ = colors;
<a name="line89"></a>  this.normalizedColors_ = null;
<a name="line90"></a>  this.setContent(this.createColorNodes());
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * @return {?string} The current selected color in hex, or null.
<a name="line96"></a> */
<a name="line97"></a>goog.ui.ColorPalette.prototype.getSelectedColor = function() {
<a name="line98"></a>  var selectedItem = /** @type {Element} */ (this.getSelectedItem());
<a name="line99"></a>  if (selectedItem) {
<a name="line100"></a>    var color = goog.style.getStyle(selectedItem, &#39;background-color&#39;);
<a name="line101"></a>    return goog.ui.ColorPalette.parseColor_(color);
<a name="line102"></a>  } else {
<a name="line103"></a>    return null;
<a name="line104"></a>  }
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * Sets the selected color.  Clears the selection if the argument is null or
<a name="line110"></a> * can&#39;t be parsed as a color.
<a name="line111"></a> * @param {?string} color The color to set as selected; null clears the
<a name="line112"></a> *     selection.
<a name="line113"></a> */
<a name="line114"></a>goog.ui.ColorPalette.prototype.setSelectedColor = function(color) {
<a name="line115"></a>  var hexColor = goog.ui.ColorPalette.parseColor_(color);
<a name="line116"></a>  if (!this.normalizedColors_) {
<a name="line117"></a>    this.normalizedColors_ = goog.array.map(this.colors_, function(color) {
<a name="line118"></a>      return goog.ui.ColorPalette.parseColor_(color);
<a name="line119"></a>    });
<a name="line120"></a>  }
<a name="line121"></a>  this.setSelectedIndex(hexColor ?
<a name="line122"></a>      goog.array.indexOf(this.normalizedColors_, hexColor) : -1);
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * @return {Array.&lt;Node&gt;} An array of DOM nodes for each color.
<a name="line128"></a> * @protected
<a name="line129"></a> */
<a name="line130"></a>goog.ui.ColorPalette.prototype.createColorNodes = function() {
<a name="line131"></a>  return goog.array.map(this.colors_, function(color) {
<a name="line132"></a>    var swatch = this.getDomHelper().createDom(&#39;div&#39;, {
<a name="line133"></a>        &#39;class&#39;: goog.getCssName(this.getRenderer().getCssClass(),
<a name="line134"></a>            &#39;colorswatch&#39;),
<a name="line135"></a>        &#39;style&#39;: &#39;background-color:&#39; + color
<a name="line136"></a>      });
<a name="line137"></a>    swatch.title = color.charAt(0) == &#39;#&#39; ?
<a name="line138"></a>        &#39;RGB (&#39; + goog.color.hexToRgb(color).join(&#39;, &#39;) + &#39;)&#39; : color;
<a name="line139"></a>    return swatch;
<a name="line140"></a>  }, this);
<a name="line141"></a>};
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * Takes a string, attempts to parse it as a color spec, and returns a
<a name="line146"></a> * normalized hex color spec if successful (null otherwise).
<a name="line147"></a> * @param {?string} color String possibly containing a color spec; may be null.
<a name="line148"></a> * @return {?string} Normalized hex color spec, or null if the argument can&#39;t
<a name="line149"></a> *     be parsed as a color.
<a name="line150"></a> * @private
<a name="line151"></a> */
<a name="line152"></a>goog.ui.ColorPalette.parseColor_ = function(color) {
<a name="line153"></a>  if (color) {
<a name="line154"></a>    /** @preserveTry */
<a name="line155"></a>    try {
<a name="line156"></a>      return goog.color.parse(color).hex;
<a name="line157"></a>    } catch (ex) {
<a name="line158"></a>      // Fall through.
<a name="line159"></a>    }
<a name="line160"></a>  }
<a name="line161"></a>  return null;
<a name="line162"></a>};
</pre>


</body>
</html>
